<template>
  <div class="login-view">
    <Header />
    
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h2>欢迎登录</h2>
          <p>请输入您的账号和密码</p>
        </div>
        
        <el-form 
          :model="form" 
          :rules="rules" 
          ref="loginForm" 
          class="login-form"
        >
          <el-form-item prop="phone">
            <el-input 
              v-model="form.phone" 
              placeholder="请输入手机号"
              size="large"
              :prefix-icon="Iphone"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="请输入密码"
              size="large"
              :prefix-icon="Lock"
              show-password
            />
          </el-form-item>
          
          <el-form-item>
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <el-link type="primary" :underline="false" class="forgot-password">
              忘记密码？
            </el-link>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p>还没有账号？
            <el-link type="primary" :underline="false" @click="goToRegister">
              立即注册
            </el-link>
          </p>
          
          <div class="social-login">
            <el-divider>其他登录方式</el-divider>
            <div class="social-buttons">
              <el-button circle size="large" class="wechat-btn">
                <el-icon><ChatDotRound /></el-icon>
              </el-button>
              <el-button circle size="large" class="qq-btn">
                <el-icon><Iphone /></el-icon>
              </el-button>
              <el-button circle size="large" class="weibo-btn">
                <el-icon><Notification /></el-icon>
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <Footer />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, ChatDotRound, Iphone, Notification } from '@element-plus/icons-vue'
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import { login } from '@/api/auth.js'

const router = useRouter()
const loginForm = ref()
const loading = ref(false)
const rememberMe = ref(false)

const form = reactive({
  phone: '',
  password: ''
})

const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    await loginForm.value.validate()
    loading.value = true
    
    // 调用后端登录API
    const response = await login({
      phone: form.phone,
      password: form.password
    })
    
    // 登录成功，保存token和用户信息
    const { token, userInfo } = response.data
    localStorage.setItem('token', token)
    localStorage.setItem('userInfo', JSON.stringify(userInfo))
    
    if (rememberMe.value) {
      // 如果选择了记住我，可以设置更长的过期时间
      // 这里简单实现，实际项目中可以使用cookie或其他方式
    }
    
    ElMessage.success('登录成功！')
    loading.value = false
    router.push('/')
  } catch (error) {
    loading.value = false
    // 更具体的错误提示
    if (error.response && error.response.data && error.response.data.detail) {
      ElMessage.error(error.response.data.detail)
    } else if (error.response && error.response.data && error.response.data.non_field_errors) {
      ElMessage.error(error.response.data.non_field_errors[0])
    } else if (error.message === 'Network Error') {
      ElMessage.error('网络错误，请检查您的网络连接')
    } else {
      ElMessage.error('登录失败，用户名或密码不正确')
    }
    console.error('Login error:', error)
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-view {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.login-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-card {
  background: white;
  padding: 40px;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  margin-bottom: 10px;
  font-size: 28px;
}

.login-header p {
  color: #666;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

.forgot-password {
  float: right;
}

.login-btn {
  width: 100%;
  border-radius: 8px;
  height: 48px;
  font-size: 16px;
}

.login-footer {
  text-align: center;
}

.login-footer p {
  color: #666;
  margin-bottom: 20px;
}

.social-login {
  margin-top: 20px;
}

.social-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.wechat-btn {
  background-color: #07C160;
  color: white;
}

.wechat-btn:hover {
  background-color: #06AE56;
}

.qq-btn {
  background-color: #12B7F5;
  color: white;
}

.qq-btn:hover {
  background-color: #0FA5DC;
}

.weibo-btn {
  background-color: #E6162D;
  color: white;
}

.weibo-btn:hover {
  background-color: #CF1429;
}
</style>